package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Shop;
import dao.DaoHelper;
import dao.ShopDao;

public class ShopDaoImp implements ShopDao {
	
	private static ShopDaoImp shopDao = new ShopDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private ShopDaoImp(){}

	public static ShopDaoImp getInstance()
	{
		return shopDao;
	}

	@Override
	public boolean save(Shop shop) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("insert into shop value(?, ?, ?)");
			stmt.setInt(1, shop.getId());
			stmt.setString(2, shop.getName());
			stmt.setString(3, shop.getLocation());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

	@Override
	public List<Shop> findAll() {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		int i = 0;
		String n = null;
		String l = null;
		Shop shop = null;
		ArrayList<Shop> list = new ArrayList<Shop>();
		
		try {	
			stmt=con.prepareStatement("select * from shop");
			result=stmt.executeQuery();
			while (result.next()) {
				i = result.getInt(1);
				n = result.getString(2);
				l = result.getString(3);
				shop = new Shop(i, n, l);
				list.add(shop);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return list;
	}

	@Override
	public Shop findById(int id) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		int i = 0;
		String n = null;
		String l = null;
		Shop shop = null;
		
		try {	
			stmt=con.prepareStatement("select * from shop where id = ?");
			stmt.setInt(1, id);
			result=stmt.executeQuery();
			while (result.next()) {
				i = result.getInt(1);
				n = result.getString(2);
				l = result.getString(3);
				shop = new Shop(i, n, l);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}

		return shop;
	}

	@Override
	public boolean updateById(Shop shop) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("update shop set name = ?, location = ? where id = ?");
			stmt.setString(1, shop.getName());
			stmt.setString(2, shop.getLocation());
			stmt.setInt(3, shop.getId());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

	@Override
	public boolean removeById(int id) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		boolean success = true;

		try {	
			stmt=con.prepareStatement("delete from shop where id = ?");
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			success = false;
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
		}

		return success;
	}

}
